package edu.uoc.tdp.pac4.client.pantalles;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JComboBox;

import edu.uoc.tdp.pac4.client.ClientRMI;
import edu.uoc.tdp.pac4.common.beans.Reparacio;
import edu.uoc.tdp.pac4.common.beans.Reparacio.Estat;
import edu.uoc.tdp.pac4.common.beans.S1Usuari;
import edu.uoc.tdp.pac4.common.beans.S2_2CarTrademark;
import edu.uoc.tdp.pac4.common.interfaces.S2Interface;
import edu.uoc.tdp.pac4.common.utils.TDSLanguageUtils;
import edu.uoc.tdp.pac4.common.beans.S2Client;
import edu.uoc.tdp.pac4.common.exception.ExcepcioClientRMI;

import javax.swing.border.EtchedBorder;


public class S2AltaSolicitud extends JPanel {
	private JTextField textFieldNomClient;
	private JTextField textFieldMatricula;
	private JTextField textFieldMarca;
	private JTextField textFieldModel;
	private JTextField textFieldObservacions;
	private JTextField textFieldNumPolissa;
	
	
	private JTextField textFieldDNIcerca;
	private JTextField textFieldLlinatges;
	
	private final JComboBox <String> comboBox_1Assaguradora;
	
	private final JFrame parent;
	private final S2Interface s2Interface;
	private final S1Usuari usuariConnectat;
	
	private ClientRMI remotS1;
	private S1PanelInicial panelInicial;
	
	private final String arrayAssaguradores [] = {"", "MAPFRE", "ZURICH", "REGAL", "LIBERTY"};

	/**
	 * Create the panel.
	 * @param usuariConnect 
	 * @param s2Interfac 
	 */
	public S2AltaSolicitud(JFrame frame, S2Interface s2Interface, S1Usuari usuariConnect) {
		
		parent = frame;
		this.s2Interface=s2Interface;
		usuariConnectat = usuariConnect;
		
		JPanel panel = new JPanel();
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 430, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 389, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(50, Short.MAX_VALUE))
		);
		
		JLabel lblDni = new JLabel(TDSLanguageUtils.getMessage("LBL_NIF"));
		
		textFieldDNIcerca = new JTextField();
		textFieldDNIcerca.setColumns(10);
		
		JButton btnCerca = new JButton(TDSLanguageUtils.getMessage("BTN_CONSULTA"));
		btnCerca.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					
					S2Client client = S2AltaSolicitud.this.s2Interface.getClientPerNIF(textFieldDNIcerca.getText());
					
					System.out.println(client);
					
					if (client==null) {
						JOptionPane.showMessageDialog(S2AltaSolicitud.this, 
								TDSLanguageUtils.getMessage("MSG_CLIENTNOEXISTEIX"), "SmartRepair", JOptionPane.ERROR_MESSAGE);

					} else {
						textFieldNomClient.setText(client.getNom());
						textFieldLlinatges.setText(client.getCognoms());
						textFieldMatricula.setText(client.getMatriculaCotxe());
						textFieldModel.setText(client.getModelCotxe());
						
						S2_2CarTrademark car = S2AltaSolicitud.this.s2Interface.getMarcaPerId(client.getIdmarca());
						
						textFieldMarca.setText(car.GetName());
						

					}
					
					
					
					
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(S2AltaSolicitud.this, 
							TDSLanguageUtils.getMessage("MSG_ERRORCONS"), "SmartRepair", JOptionPane.ERROR_MESSAGE);
					
					e1.printStackTrace();
				}
				
			
			}
		});
		
		
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblDni)
					.addGap(27)
					.addComponent(textFieldDNIcerca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 114, Short.MAX_VALUE)
					.addComponent(btnCerca)
					.addGap(86))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldDNIcerca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblDni)
						.addComponent(btnCerca))
					.addContainerGap(62, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		
		JLabel lblNomClient = new JLabel(TDSLanguageUtils.getMessage("LBL_NOM"));
		
		textFieldNomClient = new JTextField();
		textFieldNomClient.setEditable(false);
		textFieldNomClient.setColumns(10);
		
		JLabel lblMatrcula = new JLabel(TDSLanguageUtils.getMessage("LBL_MATRICULA"));
		
		textFieldMatricula = new JTextField();
		textFieldMatricula.setEditable(false);
		textFieldMatricula.setColumns(10);
		
		JLabel lblMarca = new JLabel(TDSLanguageUtils.getMessage("LBL_MARCA"));
		
		textFieldMarca = new JTextField();
		textFieldMarca.setEditable(false);
		textFieldMarca.setColumns(10);
		
		JLabel lblModel = new JLabel(TDSLanguageUtils.getMessage("LBL_MODEL"));
		
		textFieldModel = new JTextField();
		textFieldModel.setEditable(false);
		textFieldModel.setColumns(10);
		
		JLabel lblObservacions = new JLabel(TDSLanguageUtils.getMessage("LBL_OBS"));
		
		textFieldObservacions = new JTextField();
		textFieldObservacions.setColumns(10);
		
		JButton btnAlta = new JButton(TDSLanguageUtils.getMessage("BTN_ALTA"));
		btnAlta.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
			
			
			
			try {
				S2Client c = S2AltaSolicitud.this.s2Interface.getClientPerNIF(textFieldDNIcerca.getText());
					
				
					
				Reparacio r = new Reparacio ();
				
				r.setObservacions(textFieldObservacions.getText());
				r.setDataAlta(new java.util.Date ());
				r.setEstat(Estat.PDT_ACCEPTAR);
				r.setidTaller(usuariConnectat.getTaller());
				r.setNumClient(c.getNumClient());
				r.setNumPoliza(textFieldNumPolissa.getText());
				
				//Llegim combo assaguradora
				int assaguradora = comboBox_1Assaguradora.getSelectedIndex();
				r.setNomAsseguradora(arrayAssaguradores [assaguradora]);
						
						
				S2AltaSolicitud.this.s2Interface.addReparacio(r);
						
						
							
				JOptionPane.showMessageDialog(S2AltaSolicitud.this, 
						TDSLanguageUtils.getMessage("MSG_SOLINSERIDA"), "SmartRepair", JOptionPane.INFORMATION_MESSAGE);	
				
				S2AltaSolicitud panelNou = new S2AltaSolicitud(S2AltaSolicitud.this.parent, S2AltaSolicitud.this.s2Interface, usuariConnectat);
				parent.setContentPane(panelNou);
				parent.pack();
						
				} catch (RemoteException e) {
					JOptionPane.showMessageDialog(S2AltaSolicitud.this, 
							TDSLanguageUtils.getMessage("MSG_ERROR_ALTA_SOL"), "SmartRepair", JOptionPane.ERROR_MESSAGE);	
					
						e.printStackTrace();
				}
				
				
			}
		});
		
		
		JButton btnCancellar = new JButton(TDSLanguageUtils.getMessage("BTN_CANCELAR"));
		btnCancellar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				try {
					remotS1 = new ClientRMI();
					remotS1.connecta();
		        	panelInicial = new S1PanelInicial(parent, remotS1.getInterficie(), usuariConnectat);
		        	parent.setContentPane(panelInicial);
		        	parent.pack();
				} catch (ExcepcioClientRMI e1) {
					e1.printStackTrace();
				}
				
				
			}
		});
		
		JLabel lblAssaguradora = new JLabel(TDSLanguageUtils.getMessage("LBL_ASSAG"));
		
		comboBox_1Assaguradora = new JComboBox(arrayAssaguradores);
		
		JLabel lblNPolissa = new JLabel(TDSLanguageUtils.getMessage("LBL_NUMPOL"));
		
		textFieldNumPolissa = new JTextField();
		textFieldNumPolissa.setColumns(10);
		
		JLabel lblLlinatges = new JLabel(TDSLanguageUtils.getMessage("LBL_COGNOMS"));
		
		textFieldLlinatges = new JTextField();
		textFieldLlinatges.setEditable(false);
		textFieldLlinatges.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(55)
					.addComponent(btnAlta, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 107, Short.MAX_VALUE)
					.addComponent(btnCancellar, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
					.addGap(57))
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNomClient)
						.addComponent(lblObservacions)
						.addComponent(lblNPolissa)
						.addComponent(lblAssaguradora)
						.addComponent(lblModel)
						.addComponent(lblMarca)
						.addComponent(lblMatrcula)
						.addComponent(lblLlinatges))
					.addGap(42)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(textFieldLlinatges, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(225, Short.MAX_VALUE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(textFieldMatricula, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(225, Short.MAX_VALUE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(textFieldMarca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(225, Short.MAX_VALUE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(textFieldModel)
							.addGap(225))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(comboBox_1Assaguradora, 0, 86, Short.MAX_VALUE)
							.addGap(225))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(textFieldNumPolissa, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(225, Short.MAX_VALUE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(textFieldObservacions, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE)
								.addComponent(textFieldNomClient, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(36))))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(31)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNomClient)
						.addComponent(textFieldNomClient, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblLlinatges)
						.addComponent(textFieldLlinatges, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMatrcula)
						.addComponent(textFieldMatricula, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMarca)
						.addComponent(textFieldMarca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblModel)
						.addComponent(textFieldModel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAssaguradora)
						.addComponent(comboBox_1Assaguradora, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNPolissa)
						.addComponent(textFieldNumPolissa, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(textFieldObservacions, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblObservacions))
					.addGap(29)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnAlta)
						.addComponent(btnCancellar))
					.addGap(42))
		);
		panel.setLayout(gl_panel);
		setLayout(groupLayout);

	}
}
